package com.sport.controller;

import com.google.gson.Gson;
import com.sport.pojo.dto.CreateTeamDto;
import com.sport.pojo.entity.FootballPlayer;
import com.sport.pojo.entity.Team;
import com.sport.pojo.entity.UandT;
import com.sport.pojo.vo.TeamUserVo;
import com.sport.result.Result;
import com.sport.service.TeamService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.sport.constant.MessageConstant.APPLY_TEAM_ERROR;
import static com.sport.constant.MessageConstant.TEAM_CREATE_ERROR;

/**
 * @ClassName TeamController
 * @Description TODO
 * @Author dumx
 * @Date 2023/12/15 14:36
 */
@RestController
@RequestMapping("/user/team")
@Api(tags = "球队相关接口")
public class TeamController {
    @Autowired
    private TeamService teamService;

    @ApiOperation("查询球队队员")
    @PostMapping("/showteamplayers")
    public Result showTeamPlayers(String teamId){
        List<FootballPlayer> players= teamService.showAllPlayersIn(teamId);
        Gson gson = new Gson();
        String s = gson.toJson(players);
        return Result.success(s);
    }
    @ApiOperation("查询球队信息")
    @PostMapping("/showteammessage")
    public Result showTeamMessage(String teamId){
        Team team = teamService.showTeamMessageBy(teamId);
        Gson gson = new Gson();
        String s = gson.toJson(team);
        return Result.success(s);
    }
    @ApiOperation("查询所有球队")
    @GetMapping("/showallteam")
    public Result showAllTeam(){
        List<Team> teams = teamService.showAllTeam();
        Gson gson = new Gson();
        String s = gson.toJson(teams);
        return Result.success(s);
    }
    @ApiOperation("查看球队详情")
    @GetMapping("/showteamdetails")
     public Result showTeamDetails(String teamName){
        Team team = teamService.showTeamDetails(teamName);
        Gson gson = new Gson();
        String s = gson.toJson(team);
        return Result.success(s);
    }
    @ApiOperation("查询球队排行榜")
    @GetMapping("/getteamranking")
    public Result getTeamRankList(){
        return Result.success(new Gson().toJson(teamService.getTeamRankList()));
    }
    @ApiOperation("球员申请加入球队")
    @PostMapping("/applyforteam")
    public Result applyForTeam(@RequestBody TeamUserVo teamUserVo){
        System.out.println(teamUserVo.toString());
        int info = teamService.applyFor(teamUserVo.getUserName(), teamUserVo.getTeamId());
        System.out.println(teamUserVo.getUserName());
        if (info!=0){
            return Result.success();
        }else {
            return Result.error(APPLY_TEAM_ERROR);
        }
    }
    @ApiOperation("查看球队申请列表")
    @PostMapping("/getteamapplytable")
    public Result getApplyTable(String applicationTeamId){
        return Result.success(new Gson().toJson(teamService.getApplyTable(applicationTeamId)));
    }
    @ApiOperation("队长同意申请人入队")
    @PostMapping("/applyYesUserIntoTeam")
    public Result applyYesUserIntoTeamByName(TeamUserVo teamUserVo){
        int info = teamService.applyYesUserIntoTeamByName(teamUserVo);
        if (info!=0){
            return Result.success("同意入队成功");
        }else {
            return Result.error("同意入队失败");
        }
    }
    @ApiOperation("队长拒绝申请人入队")
    @PostMapping("/applyNoUserIntoTeam")
    public Result applyNoUserIntoTeamByName(TeamUserVo teamUserVo){
        int info = teamService.applyNoUserIntoTeamByName(teamUserVo);
        if (info!=0){
            return Result.success("拒绝入队成功");
        }else {
            return Result.error("拒绝入队失败");
        }
    }
    @ApiOperation("查看联赛积分榜")
    @GetMapping("/showleaguescoreboard")
    public Result  showLeagueScoreboard(){
        return Result.success(new Gson().toJson(teamService.showLeagueScoreboard()));
    }

    @ApiOperation("查询球队")
    @GetMapping("/searchteambyname")
    public Result searchTeam(String teamName){
        List<Team> teams = teamService.searchTeam(teamName);
        Gson gson = new Gson();
        String s = gson.toJson(teams);
        return Result.success(s);
    }
    @ApiOperation("创建球队")
    @PostMapping("/createteam")
    public Result creatTeam(CreateTeamDto createTeamDto){
        int info = teamService.creatTeam(createTeamDto);
        if (info!=0){
            return Result.success();
        }else {
            return Result.error(TEAM_CREATE_ERROR);
        }
    }
}
